var member_id = ws = '';
var price = prices = 0;
var cnum = xnum = 0;
var goods_id = store_id = cart_id = remarks = '';
document.addEventListener('plusready',function(){
	member_id = plus.storage.getItem('id');
	ws = plus.webview.currentWebview();
	var goods_id = ws.goods_id;
	console.log(apiRoot+"?act=goods&op=index&member_id="+ member_id +"&goods_id="+goods_id+"&flag=1");
	$.ajax({  
		type:"get", 
		url:apiRoot+"?act=goods&op=index&member_id="+ member_id +"&goods_id="+goods_id+"&flag=1",
		dataType:'json', 
		success:function(data){ 
			console.log(JSON.stringify(data));
			if(data){ 
				$('.title').text(data.goods_info.goods_name);
				$('.price').text(data.goods_info.goods_price);
				$('.pic').attr('src', (pics + data.goods_info.store_id + '/' + data.goods_info.goods_image));			//图片 
				$('.spec').text(data.goods_info.gc_name.substr(data.goods_info.gc_name.lastIndexOf(';')+1))			//销量
				price = data.goods_info.goods_price;
				prices = data.goods_info.goods_costprice;
				goods_id = data.goods_info.goods_id;
				store_id = data.goods_info.store_id;
				if(data.travel.length > 0){
					var html = '';
					$.each(data.travel, function(k,v) {
						(v.travel_type == "成人")?(cnum = cnum + 1):(xnum = xnum + 1)
						html += '<div class="panduan" data-travel_id="'+ v.travel_id +'"><li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3">'+ v.travel_name +'</li>'+
								'<li class="mui-table-view-cell mui-media mui-col-xs-2 mui-col-sm-2">'+ v.travel_type +'</li>'+
								'<li class="mui-table-view-cell mui-media mui-col-xs-5 mui-col-sm-5 mui-ellipsis">'+ v.travel_idnumber +'</li>'+
								'<li class="mui-table-view-cell mui-media mui-col-xs-2 mui-col-sm-2">'+
									'<i class="iconfont ify7" data-type="'+ v.travel_type +'">&#xe610;</i>'+
								'</li></div>'; 
					});
//					(cnum + 1):(xnum + 1)
					$('.travel').html(html);
//					console.log(cnum+'--'+xnum);
					var money = (cnum * price) + (xnum * prices);
					$('.money').text(money.toFixed(2));
				}
			
			
			}else{
				toast('加载失败！');
			}
			
			//移除出行人
			$('.ify7').unbind('tap');
			$('.ify7').on('tap',function(){
				var _this = $(this);
				var travel_type = _this.attr('data-type');
				var rmb = (travel_type == '成人')?price:prices;
				var money = $('.money').text();
				var jine = parseFloat(money) - rmb;
				$('.money').text(jine.toFixed(2));
//				console.log(jine.toFixed(2));return;  
				_this.parent().parent().remove(); 
			})
			
			
		}, 
		error:function(e){
			toast('连接失败！'); 
		}
	});
	$('.conten').on('click',function(){
//		console.log(ws.id);return;
		var travel_ids = '';
		$('.panduan').each(function(){
			travel_ids += $(this).attr('data-travel_id') + ',';
		})
//		console.log(travel_ids);return;    
		var nwaiting = plus.nativeUI.showWaiting();//显示原生等待框 
		var webviewContent= plus.webview.create('./Personal_Addcx.html','Personal_Addcx.html',{},{travel_ids:travel_ids,spage:ws.id});//后台创建webview并打开show.html
		webviewContent.addEventListener("loaded", function() { //注册新webview的载入完成事件
	        nwaiting.close(); //新webview的载入完毕后关闭等待框
	        webviewContent.show("slide-in-right",500); //把新webview窗体显示出来，显示动画效果为速度200毫秒的右侧移入动画
	    }, false);
	}) 
	
	$('.Good').on('click',function(){
		remarks = $('.remarks').val();    //留言备注
		var order_phone = $('.order_phone').val();   //联系号码
		var order_time = $('#result').val();   //出行时间
		if(order_phone){
			if(!order_phone.match(p1)){
				toast('请输入正确的手机号码！');return;
			}
		}
		if(!order_time){
			toast('请选择出行日期');return;
		}
		var travel_ids = '';
		var nums = 0;
		$('.panduan').each(function(){
			nums++;
			travel_ids += $(this).attr('data-travel_id') + ',';
		})
		cart_id = goods_id+ '|'+nums;
		if(nums <= 0){ 
			toast('请添加出行人！');return;
		}
//		payment_type:'online',ifcart:0
//		goods_inf(cart_id);
		
		$.ajax({ 
			type:"post",
			url:apiRoot + "?act=member_buy&op=buy_step1",
			data:{
				member_id:member_id,
				cart_id:cart_id,
				ifcart:0
			},
			dataType:'json',
			success:function(data){
				console.log(JSON.stringify(data));
				if(data){
					goods_inf(data.vat_hash,order_phone,order_time,travel_ids);
				}else{
					
				}
			},
			error:function(e){
				console.log(JSON.stringify(e));
			}
		});
		
	})
},false);
  
window.addEventListener('travel',function(event){
	var data = event.detail.data;
	if(data){
		var htmls = '<div class="panduan" data-travel_id="'+ data.travel_id +'"><li class="mui-table-view-cell mui-media mui-col-xs-3 mui-col-sm-3">'+ data.travel_name +'</li>'+
						'<li class="mui-table-view-cell mui-media mui-col-xs-2 mui-col-sm-2">'+ data.travel_type +'</li>'+
						'<li class="mui-table-view-cell mui-media mui-col-xs-5 mui-col-sm-5 mui-ellipsis">'+ data.idnumber +'</li>'+
						'<li class="mui-table-view-cell mui-media mui-col-xs-2 mui-col-sm-2">'+ 
							'<i class="iconfont ify7" data-type="'+ data.travel_type +'">&#xe610;</i>'+
						'</li></div>';
		$('.travel').append(htmls);
		var zengjia = (data.travel_type == '成人')?price:prices;
		var money = $('.money').text();
		var jine = parseFloat(money) + parseFloat(zengjia); 
//		console.log(zengjia+'---'+money+'---'+jine);return;
		$('.money').text(jine.toFixed(2));
	}
	//移除出行人
	$('.ify7').unbind('tap');
	$('.ify7').on('tap',function(){
		var _this = $(this);
		var travel_type = _this.attr('data-type');
		var rmb = (travel_type == '成人')?price:prices;
		var money = $('.money').text();
		var jine = parseFloat(money) - rmb;
		$('.money').text(jine.toFixed(2));
		_this.parent().parent().remove(); 
	})
},false)

function goods_inf(vat_hash,order_phone,order_time,travel_ids){
//	console.log(apiRoot + "?act=member_buy&op=buy_step1"+member_id+'--'+vat_hash+'--'+order_phone+'--'+order_time+'--'+travel_ids);return;
	$.ajax({ 
	 	type:"post",
	 	url:apiRoot + "?act=member_buy&op=buy_step2", 
	 	data:{
	 		cart_id:cart_id,
	 		pay_name:'online',
	 		member_id:member_id,
	 		pay_message:remarks,
	 		vat_hash:vat_hash,
	 		order_phone:order_phone,
	 		travel_ids:travel_ids,
	 		order_time:order_time,
	 		ifcart:0,
	 		ordertype:2
	 	},  
	 	dataType:'json',
	 	success:function(data){
	 		console.log(JSON.stringify(data));
	 		if(data.state){
	 			$('.payment').attr('pay_sn',data.pay_sn);
	 			$('.payment').attr('order_id',data.order_key_id);
				$('.popover').show();
				$('.btn').slideDown()
	 		}else{
	 			toast(data.datas.error);
	 			ws.close();
	 		}
	 	},
	 	error:function(e){
	 		console.log(JSON.stringify(e));
	 	}
	});
}